﻿<UserControl x:Class="CodeStash.Addin.Views.SearchResultsView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:attachedProps="clr-namespace:CodeStash.Addin.Behaviours"
             xmlns:viewModels="clr-namespace:CodeStash.Addin.ViewModels"
             xmlns:conv="clr-namespace:CodeStash.Addin.ValueConverters"
             mc:Ignorable="d"
             x:Name="theView"
             d:DesignHeight="500"
             d:DesignWidth="500">





    <Grid>


        <Grid Visibility="{Binding Path=HasResults,   
                Converter={x:Static conv:BoolToVisibilityConverter.Instance},   
                ConverterParameter=true}">
            <Grid.RowDefinitions>
                <RowDefinition Height="*" />
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>

            <DataGrid Grid.Row="0"
                      BorderThickness="0"
                      ItemsSource="{Binding Results.PagedMatchedSnippets}"
                      IsReadOnly="True"
                      SelectionUnit="FullRow"
                      SelectionMode="Single"
                      SelectedItem="{Binding SelectedSnippet}"
                      AutoGenerateColumns="False"
                      GridLinesVisibility="None">


                <DataGrid.Resources>
                    
                    <conv:DecodeConverter x:Key="decodeConverter" />

                    <Style TargetType="{x:Type DataGridCell}">
                        <Setter Property="BorderBrush"
                                Value="LightGray" />
                        <Setter Property="BorderThickness"
                                Value="0.25" />
                        <Style.Triggers>
                            <Trigger Property="IsSelected"
                                     Value="True">
                                <Setter Property="BorderThickness"
                                        Value="0.25" />
                            </Trigger>
                        </Style.Triggers>
                    </Style>

                    <Style x:Key="ViewCurrentHyperLinkColumnStyle"
                           TargetType="{x:Type DataGridCell}">
                        <Setter Property="attachedProps:DataGridAttachProps.HyperlinkCommand"
                                Value="{Binding ElementName=theView, Path=DataContext.ViewCurrentCommand}" />
                    </Style>

                    <Style x:Key="InsertCurrentHyperLinkColumnStyle"
                           TargetType="{x:Type DataGridCell}">
                        <Setter Property="attachedProps:DataGridAttachProps.HyperlinkCommand"
                                Value="{Binding ElementName=theView, Path=DataContext.InsertCurrentCommand}" />
                    </Style>

                </DataGrid.Resources>

                <DataGrid.Columns>
                    <DataGridHyperlinkColumn Header="ActualCode"
                                             Binding="{Binding ActualCode}"
                                             CellStyle="{StaticResource ViewCurrentHyperLinkColumnStyle}"
                                             ContentBinding="{Binding Source={x:Static viewModels:SearchResultsViewModel.ViewLinkText}}" />
                    <DataGridHyperlinkColumn Header="ActualCode"
                                             Binding="{Binding ActualCode}"
                                             CellStyle="{StaticResource InsertCurrentHyperLinkColumnStyle}"
                                             ContentBinding="{Binding Source={x:Static viewModels:SearchResultsViewModel.InsertLinkText}}" />
                    <DataGridTextColumn Header="Id"
                                        Binding="{Binding Path=CodeSnippetId}" />
                    <DataGridTextColumn Header="Category"
                                        Binding="{Binding Path=CategoryName, Converter={StaticResource decodeConverter}}" />
                    <DataGridTextColumn Header="Language"
                                        Binding="{Binding Path=Language}" />
                    <DataGridTextColumn Header="Tags"
                                        Binding="{Binding Path=Tags, Converter={StaticResource decodeConverter}}" />
                    <DataGridTextColumn Header="Description"
                                        Binding="{Binding Path=Description, Converter={StaticResource decodeConverter}}" />
                    <DataGridTextColumn Header="Title"
                                        Binding="{Binding Path=Title, Converter={StaticResource decodeConverter}}" />
                    <DataGridTextColumn Header="Group Name"
                                        Binding="{Binding Path=GroupName, Converter={StaticResource decodeConverter}}" />
                </DataGrid.Columns>


            </DataGrid>


            <StackPanel Orientation="Horizontal"
                        Grid.Row="1"
                        Margin="5">

                <Button ToolTip="First page"
                        Margin="0,0,10,0"
                        Command="{Binding ShowFirstPageCommand}">
                    <Button.Template>
                        <ControlTemplate TargetType="{x:Type Button}">
                            <Path Data="M280,176 L303.5,159.5 303.5,191.5 z"
                                  Fill="Black"
                                  HorizontalAlignment="Left"
                                  Height="12"
                                  Margin="0"
                                  Stretch="Fill"
                                  Stroke="Black"
                                  VerticalAlignment="Center"
                                  Width="12" />
                        </ControlTemplate>
                    </Button.Template>
                </Button>


                <Label Content="Show page" />
                <ComboBox ItemsSource="{Binding TotalPages}"
                          SelectedItem="{Binding CurrentPage}"
                          Width="100">
                    <ComboBox.ItemTemplate>
                        <DataTemplate>
                            <Label Margin="5,0,0,0"
                                   Content="{Binding}" />
                        </DataTemplate>
                    </ComboBox.ItemTemplate>
                </ComboBox>

                <Button ToolTip="Last page"
                        Margin="10,0,0,0"
                        Command="{Binding ShowLastPageCommand}">
                    <Button.Template>
                        <ControlTemplate TargetType="{x:Type Button}">
                            <Path Data="M312,160 L335.5,175.5 311.5,191.5 z"
                                  Fill="Black"
                                  Height="12"
                                  Width="12"
                                  Margin="0"
                                  Stretch="Fill"
                                  Stroke="Black"
                                  HorizontalAlignment="right"
                                  VerticalAlignment="Center" />
                        </ControlTemplate>
                    </Button.Template>
                </Button>

            </StackPanel>

        </Grid>



        <Label Content="No Search Results"
               Visibility="{Binding Path=HasResults,   
                Converter={x:Static conv:BoolToVisibilityConverter.Instance},   
                ConverterParameter=false}"
               FontWeight="Bold"
               HorizontalAlignment="Center"
               HorizontalContentAlignment="Center"
               VerticalAlignment="Center"
               VerticalContentAlignment="Center" />

    </Grid>




</UserControl>
    